//
//  UserViewController.m
//  享动
//
//  Created by darker on 16/4/9.
//  Copyright © 2016年 HHUI. All rights reserved.
//

#import "UserViewController.h"
#import "UserTableViewCell.h"
#import "UserNameViewController.h"

@interface UserViewController () <UITableViewDelegate, UITableViewDataSource, UIPickerViewDataSource, UIPickerViewDelegate, UITextFieldDelegate, UIImagePickerControllerDelegate, UINavigationControllerDelegate>

@property (weak, nonatomic) IBOutlet UITableView *tableView;

@property (nonatomic, strong) UserTableViewCell *cell;

// 头像
@property (nonatomic, strong) UIImageView *headImageView;

//  性别 身高 体重 生日 城市 toolBar
@property (weak, nonatomic) IBOutlet UIToolbar *genderToolBar;

@property (weak, nonatomic) IBOutlet UIToolbar *heightToolbar;

@property (weak, nonatomic) IBOutlet UIToolbar *weightToolBar;

@property (weak, nonatomic) IBOutlet UIToolbar *birthdayToolBar;

@property (weak, nonatomic) IBOutlet UIToolbar *cityToolBar;

// 年份 身高 体重 性别 城市 pickerView
@property (weak, nonatomic) IBOutlet UIPickerView *yearPickerView;

@property (weak, nonatomic) IBOutlet UIPickerView *heightPickerView;

@property (weak, nonatomic) IBOutlet UIPickerView *weightPickerView;

@property (weak, nonatomic) IBOutlet UIPickerView *genderPickerView;

@property (weak, nonatomic) IBOutlet UIPickerView *cityPickerView;


// 性别 月份 天数 年份 身高 体重  省份 数组 / 城市字典
@property (nonatomic, strong) NSArray *genderArray;

@property (nonatomic, strong) NSArray *monthArray;

@property (nonatomic, strong) NSArray *dayArray;

@property (nonatomic, strong) NSMutableArray *yearArray;

@property (nonatomic, strong) NSMutableArray *heightArray;

@property (nonatomic, strong) NSMutableArray *weightArray;

@property (nonatomic, strong) NSArray *provinceArray;

@property (nonatomic, strong) NSDictionary *cityDic;

// 装载 所有PickerView 的视图
@property (weak, nonatomic) IBOutlet UIView *AllPickerView;

// 给头像视图 添加一个长按手势
@property (nonatomic, strong) UITapGestureRecognizer *tap;

@end

@implementation UserViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    _AllPickerView.frame = CGRectMake(0, KHEIGHT, KWIDTH, 290);
    
    _tableView.frame = CGRectMake(0, 0, KWIDTH, KHEIGHT - 64);
    _tableView.delegate = self;
    _tableView.dataSource = self;
    
    [_tableView registerClass:[UserTableViewCell class] forCellReuseIdentifier:@"cell"];
    
    // 性别
    _genderPickerView.frame = CGRectMake(0, 30, KWIDTH, 260);
    _genderPickerView.showsSelectionIndicator = YES;
    _genderPickerView.delegate = self;
    _genderPickerView.dataSource = self;
    _genderArray = [NSArray array];
    _genderArray = @[@"男", @"女"];
    
    // 生日
    _yearPickerView.frame = CGRectMake(0, 30, KWIDTH, 260);
    _yearPickerView.showsSelectionIndicator = YES;
    _yearPickerView.delegate = self;
    _yearPickerView.dataSource = self;
    
    _monthArray = [NSArray array];
    _monthArray = @[@"1", @"2", @"3", @"4", @"5", @"6", @"7", @"8", @"9", @"10", @"11", @"12"];
    
    _dayArray = [NSArray array];
    _dayArray = @[@"1", @"2", @"3", @"4", @"5", @"6", @"7", @"8", @"9", @"10", @"11", @"12", @"13", @"14", @"15", @"16", @"17", @"18", @"19", @"20", @"21", @"22", @"23", @"24", @"25", @"26", @"28", @"29", @"30", @"31"];
    
    _yearArray = [NSMutableArray array];
    for (int i = 1900; i < 2017; i++)
    {
        [_yearArray addObject:[NSString stringWithFormat:@"%d", i]];
    }
    _cityDic = [NSDictionary dictionary];
    
    // 身高
    _heightPickerView.frame = CGRectMake(0, 290, KWIDTH, 260);
    _heightPickerView.showsSelectionIndicator = YES;
    _heightPickerView.delegate = self;
    _heightPickerView.dataSource = self;
    
    _heightArray = [NSMutableArray array];
    for (int i = 30; i < 200; i++)
    {
        [_heightArray addObject:[NSString stringWithFormat:@"%d", i]];
    }
    
    // 体重
    _weightPickerView.frame = CGRectMake(0, 290, KWIDTH, 260);
    _weightPickerView.showsSelectionIndicator = YES;
    _weightPickerView.delegate = self;
    _weightPickerView.dataSource = self;
    
    _weightArray = [NSMutableArray array];
    for (int i = 5; i < 200; i++)
    {
        [_weightArray addObject:[NSString stringWithFormat:@"%d", i]];
    }

    // 省份
    _cityPickerView.frame = CGRectMake(0, 290, KWIDTH, 260);
    _cityPickerView.delegate = self;
    _cityPickerView.dataSource = self;
    _cityPickerView.showsSelectionIndicator = YES;

    //省
    _provinceArray = @[@"北京省", @"广西省", @"广东省", @"河北省",@"福建省"];
    //市
    _cityDic = @{
                 @"北京省":@[@"朝阳区", @"东城区", @"西城区"],
                 @"广西省":@[@"桂林市", @"南宁市"],
                 @"广东省":@[@"惠州市", @"广州市", @"深圳市",@"东莞市"],
                 @"河北省":@[@"石家庄",@"保定市", @"秦皇岛", @"唐山市", @"邯郸市",@"邢台市", @"沧州市", @"承德市", @"廊坊市", @"衡水市",@"张家口"],
                 @"福建省":@[@"福州市", @"宁德市", @"南平市", @"莆田市",@"泉州市",@"龙岩市",@"漳州市",@"三明市"]
                 };
    
    // 加载界面隐藏所有的toolBar
    _genderToolBar.frame = CGRectMake(0, 350, KWIDTH, 30);
    _weightToolBar.frame = CGRectMake(0, 350, KWIDTH, 30);
    _heightToolbar.frame = CGRectMake(0, 350, KWIDTH, 30);
    _birthdayToolBar.frame = CGRectMake(0, 350, KWIDTH, 30);
    _cityToolBar.frame = CGRectMake(0, 350, KWIDTH, 30);

}

#pragma mark --- 手势 触发的方法 ---
- (void) tapAction:(UITapGestureRecognizer *) tap
{
    dispatch_async(dispatch_get_main_queue(), ^{
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:nil preferredStyle:UIAlertControllerStyleAlert];
        
        UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
        
        UIAlertAction *photoAction = [UIAlertAction actionWithTitle:@"照相" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
            {
                UIImagePickerController *picker =[[UIImagePickerController alloc]init];
                picker.allowsEditing = YES;
                picker.sourceType =  UIImagePickerControllerSourceTypeCamera;
                
                // 设置进入相册后 选取照片的代理 (执行 拍摄完成之后  或者选择完照片之后 自动调用的代理方法)
                picker.delegate = self;
                [self presentViewController:picker animated:YES completion:nil];
            }
            else
            {
                NSLog(@"没有相机");
            }
        }];
        
        UIAlertAction *selectPhotoAction = [UIAlertAction actionWithTitle:@"从相册选择" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            
            if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary])
            {
                UIImagePickerController *picker =[[UIImagePickerController alloc]init];
                picker.allowsEditing = YES;
                picker.sourceType =  UIImagePickerControllerSourceTypePhotoLibrary;
                
                // 设置进入相册后 选取照片的代理 (执行 拍摄完成之后  或者选择完照片之后 自动调用的代理方法)
                picker.delegate = self;
                [self presentViewController:picker animated:YES completion:nil];
            }
            else
            {
                NSLog(@"没有相册");
            }
        }];
        [alert addAction:selectPhotoAction];
        [alert addAction:photoAction];
        [alert addAction:cancelAction];
        [self presentViewController:alert animated:YES completion:nil];
    });
}

#pragma mark --- ( 执行 拍摄完成之后  或者选择完照片之后 自动调用的代理方法 ) ---
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    // 选取完毕之后  返回到原来的页面
    [self dismissViewControllerAnimated:YES completion:nil];
    // 将所选的照片赋值给 我们自己的imageView.image
    _headImageView.image = [info objectForKey:UIImagePickerControllerOriginalImage];
    UIImage *img = [info objectForKey:UIImagePickerControllerOriginalImage];
    NSData *data =  UIImagePNGRepresentation(img);
    [[NSUserDefaults standardUserDefaults] setObject:data forKey:@"headImage"];
}

#pragma mark --- 城市取消button ---
- (IBAction)cityCancelButton:(id)sender {
    [self cancel];
}

#pragma mark --- 城市完成button ---
- (IBAction)cityEndButton:(id)sender {
    NSInteger provinceRow = [_cityPickerView selectedRowInComponent:0];
    NSString *provinceName = _provinceArray[provinceRow];
    NSArray *cityName = [_cityDic objectForKey:provinceName];
    NSInteger cityRow = [_cityPickerView selectedRowInComponent:1];
    
    [[NSUserDefaults standardUserDefaults] setObject:_provinceArray[provinceRow] forKey:@"province"];
    [[NSUserDefaults standardUserDefaults] setObject:cityName[cityRow] forKey:@"city"];
    
    [UIView animateWithDuration:0.5 animations:^
     {
         _AllPickerView.frame = CGRectMake(0, KHEIGHT, KWIDTH, 290);
     }];
    
    [_tableView reloadData];
}

#pragma mark --- 生日取消button ---
- (IBAction)birthDayCancelButton:(id)sender {
    [self cancel];
}

#pragma mark --- 生日完成button ---
- (IBAction)birthDayEndButton:(id)sender {
    NSInteger yearRow = [_yearPickerView selectedRowInComponent:0];
    NSInteger monthRow = [_yearPickerView selectedRowInComponent:1];
    NSInteger dayRow = [_yearPickerView selectedRowInComponent:2];
    [[NSUserDefaults standardUserDefaults] setObject:_yearArray[yearRow] forKey:@"year"];
    [[NSUserDefaults standardUserDefaults] setObject:_monthArray[monthRow] forKey:@"month"];
    [[NSUserDefaults standardUserDefaults] setObject:_dayArray[dayRow] forKey:@"day"];
    [UIView animateWithDuration:0.5 animations:^
     {
         _AllPickerView.frame = CGRectMake(0, KHEIGHT, KWIDTH, 290);
     }];
    
    [_tableView reloadData];
}

#pragma mark --- 体重完成button ---
- (IBAction)weightEndButton:(id)sender {
    NSInteger weightRow = [_weightPickerView selectedRowInComponent:0];
    [[NSUserDefaults standardUserDefaults] setObject:_weightArray[weightRow] forKey:@"weight"];
    [UIView animateWithDuration:0.5 animations:^
     {
         _AllPickerView.frame = CGRectMake(0, KHEIGHT, KWIDTH, 290);
     }];
    
    [_tableView reloadData];
}

#pragma mark --- 体重取消button ---
- (IBAction)weightCancelButton:(id)sender {
    [self cancel];
}

#pragma mark --- 身高完成button ---
- (IBAction)heightEndButton:(id)sender {
    NSInteger heightRow = [_heightPickerView selectedRowInComponent:0];
    [[NSUserDefaults standardUserDefaults] setObject:_heightArray[heightRow] forKey:@"height"];
    [UIView animateWithDuration:0.5 animations:^
     {
         _AllPickerView.frame = CGRectMake(0, KHEIGHT, KWIDTH, 290);
     }];
    
    [_tableView reloadData];
}

#pragma mark --- 身高取消button ---
- (IBAction)heightCancelButton:(id)sender {
    [self cancel];
}

#pragma mark --- 性别完成button ---
- (IBAction)genderEndButton:(id)sender
{
    NSInteger row = [_genderPickerView selectedRowInComponent:0];
    
    [[NSUserDefaults standardUserDefaults] setObject:_genderArray[row] forKey:@"gender"];

    [UIView animateWithDuration:0.5 animations:^
    {
        _AllPickerView.frame = CGRectMake(0, KHEIGHT, KWIDTH, 290);
    }];
    
    [_tableView reloadData];
}
#pragma mark --- 取消button ---
- (IBAction)cancelButton:(id)sender
{
    [self cancel];
}

#pragma mark --- 取消button 执行一样的方法 ---
- (void)cancel
{
    [UIView animateWithDuration:0.5 animations:^
     {
         _AllPickerView.frame = CGRectMake(0, KHEIGHT, KWIDTH, 290);
     }];
}

#pragma mark --- 返回tableView 每个分区的行数 ---
- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0)
    {
        return 1;
    }
    else if (section == 1)
    {
        return 6;
    }
    else if (section == 2)
    {
        return 2;
    }
    return 0;
}

#pragma mark --- 返回tableView 分区个数 ---
- (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView
{
    return 3;
}

#pragma mark --- 返回tableView 每行高度 ---
- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 50;
}

#pragma mark --- 返回tableViewCell ---
- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    _cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    _cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    
    if (indexPath.section == 0)
    {
        if (indexPath.row == 0)
        {
            _headImageView = [[UIImageView alloc] initWithFrame:CGRectMake(KWIDTH - 70, 5, 40, 40)];
            _headImageView.backgroundColor = [UIColor grayColor];
            _headImageView.layer.cornerRadius = 20;
            _headImageView.userInteractionEnabled = YES;
            _headImageView.clipsToBounds = YES;
            _headImageView.image = [UIImage imageWithData:[[NSUserDefaults standardUserDefaults] objectForKey:@"headImage"]];
            _tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
            [_headImageView addGestureRecognizer:_tap];
            _cell.leftLabel.text = @"更换头像";
            [_cell.rightLabel removeFromSuperview];
            [_cell addSubview:_headImageView];
            
        }
    }
    if (indexPath.section == 1)
    {
        if (indexPath.row == 0)
        {
            _cell.leftLabel.text = @"昵称";
            _cell.rightLabel.text = [NSString stringWithFormat:@"%@", [[NSUserDefaults standardUserDefaults] objectForKey:@"userName"]];

        }
        else if (indexPath.row == 1)
        {
            _cell.leftLabel.text = @"性别";
            _cell.rightLabel.text = [[NSUserDefaults standardUserDefaults] objectForKey:@"gender"];
            
        }
        else if (indexPath.row == 2)
        {
            _cell.leftLabel.text = @"身高";
            _cell.rightLabel.text = [NSString stringWithFormat:@"%@cm", [[NSUserDefaults standardUserDefaults] objectForKey:@"height"]];;
            
        }
        else if (indexPath.row == 3)
        {
            _cell.leftLabel.text = @"体重";
            _cell.rightLabel.text = [NSString stringWithFormat:@"%@kg", [[NSUserDefaults standardUserDefaults] objectForKey:@"weight"]];
            
        }
        else if (indexPath.row == 4)
        {
            _cell.leftLabel.text = @"生日";
            _cell.rightLabel.text = [NSString stringWithFormat:@"%@ %@ %@",[[NSUserDefaults standardUserDefaults] objectForKey:@"year"],[[NSUserDefaults standardUserDefaults] objectForKey:@"month"],[[NSUserDefaults standardUserDefaults] objectForKey:@"day"] ];

        }
        else if (indexPath.row == 5)
        {
            _cell.leftLabel.text = @"所在地";
            _cell.rightLabel.text = [NSString stringWithFormat:@"%@ %@", [[NSUserDefaults standardUserDefaults] objectForKey:@"province"], [[NSUserDefaults standardUserDefaults] objectForKey:@"city"]];
        }
    }
    if (indexPath.section == 2)
    {
        if (indexPath.row == 0)
        {
            _cell.leftLabel.text = @"介绍";
        }
        else if (indexPath.row == 1)
        {
            _cell.introduceTextField.frame = CGRectMake(0, 0, KWIDTH, 50);
            _cell.introduceTextField.placeholder = @"个人介绍...";
            _cell.introduceTextField.delegate = self;
            [_cell addSubview:_cell.introduceTextField];
            // 给键盘添加两个方法
            [self registerForKeyboardNotifications];
        }
    }
    return _cell;
}

#pragma mark --- 给键盘添加两个方法 ---
- (void) registerForKeyboardNotifications
{
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWasShown:) name:UIKeyboardWillShowNotification object:nil];
    
    [[NSNotificationCenter defaultCenter]  addObserver:self selector:@selector(keyboardWasHidden:) name:UIKeyboardWillHideNotification object:nil];
}

#pragma mark --- 键盘出现时 ---
- (void) keyboardWasShown:(NSNotification *) notif
{
    NSDictionary *info = [notif userInfo];
    NSValue *value = [info objectForKey:UIKeyboardFrameBeginUserInfoKey];
    CGSize keyboardSize = [value CGRectValue].size;
    _tableView.frame = CGRectMake(0, - keyboardSize.height, KWIDTH, KHEIGHT);
}

#pragma mark --- 键盘消失时 ---
- (void) keyboardWasHidden:(NSNotification *) notif
{
    _tableView.frame = CGRectMake(0, 0, KWIDTH, KHEIGHT - 64);
}

#pragma mark --- 回收键盘 ---
- (BOOL) textFieldShouldReturn:(UITextField *)textField
{
    [_cell.introduceTextField resignFirstResponder];
    return YES;
}

- (void) touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [_cell.introduceTextField resignFirstResponder];
}

#pragma mark --- 返回表尾高度 ---
- (CGFloat) tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    if (section == 0 || section == 1)
    {
        return 5.0;
    }
    return 0;
}

#pragma mark --- 点击cell 触发的方法 ---
- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 1)
    {
        if (indexPath.row == 0)
        {
            // 弹出一个view  修改昵称
            UserNameViewController *userNameVc = [[UserNameViewController alloc] init];
            _cell = [tableView cellForRowAtIndexPath:indexPath];
            userNameVc.userName = _cell.rightLabel.text;
            [self.navigationController pushViewController:userNameVc animated:YES];
            __weak UserViewController *blockSelf = self;

            userNameVc.myUserName = ^(NSString *string){
                // 只有调用block的时候才运行这行代码
                blockSelf.cell.rightLabel.text = string;
                [[NSUserDefaults standardUserDefaults] setObject:string forKey:@"userName"];
            };
        }
        if (indexPath.row == 1)
        {
            // 弹出性别pickerView 隐藏其他pickerView
            [UIView animateWithDuration:0.5 animations:^{
                
                [self setAllPickerViewFrame:CGRectMake(0, KHEIGHT - 290, KWIDTH, 290) YearFrame:CGRectMake(0, _AllPickerView.frame.size.height, KWIDTH, 260) GenderFrame:CGRectMake(0, 30, KWIDTH, 260) WeightFrame:CGRectMake(0, _AllPickerView.frame.size.height, KWIDTH, 260) HeightFrame:CGRectMake(0, _AllPickerView.frame.size.height, KWIDTH, 260) CityFrame:CGRectMake(0, _AllPickerView.frame.size.height, KWIDTH, 260) GenderTool:CGRectMake(0, 0, KWIDTH, 30) WeightTool:CGRectMake(0, 350, KWIDTH, 30) HeightTool:CGRectMake(0, 350, KWIDTH, 30) BirthdayTool:CGRectMake(0, 350, KWIDTH, 30) CityTool:CGRectMake(0, 350, KWIDTH, 30)];
            }];
        }
        if (indexPath.row == 2)
        {
            // 弹出身高pickerView 隐藏其他pickerView
            [UIView animateWithDuration:0.5 animations:^{
                
                [self setAllPickerViewFrame:CGRectMake(0, KHEIGHT - 290, KWIDTH, 290) YearFrame:CGRectMake(0, _AllPickerView.frame.size.height, KWIDTH, 260) GenderFrame:CGRectMake(0, _AllPickerView.frame.size.height, KWIDTH, 260) WeightFrame:CGRectMake(0, _AllPickerView.frame.size.height, KWIDTH, 260) HeightFrame:CGRectMake(0, 30, KWIDTH, 260) CityFrame:CGRectMake(0, _AllPickerView.frame.size.height, KWIDTH, 260) GenderTool:CGRectMake(0, 290, KWIDTH, 30) WeightTool:CGRectMake(0, 290, KWIDTH, 30) HeightTool:CGRectMake(0, 0, KWIDTH, 30) BirthdayTool:CGRectMake(0, 290, KWIDTH, 30) CityTool:CGRectMake(0, 290, KWIDTH, 30)];
            }];
        }
        if (indexPath.row == 3)
        {
            // 弹出体重pickerView 隐藏其他pickerView
            [UIView animateWithDuration:0.5 animations:^{
                [self setAllPickerViewFrame:CGRectMake(0, KHEIGHT - 290, KWIDTH, 290) YearFrame:CGRectMake(0, _AllPickerView.frame.size.height, KWIDTH, 260) GenderFrame:CGRectMake(0, _AllPickerView.frame.size.height, KWIDTH, 260) WeightFrame:CGRectMake(0, 30, KWIDTH, 260) HeightFrame:CGRectMake(0, _AllPickerView.frame.size.height, KWIDTH, 260) CityFrame: CGRectMake(0, _AllPickerView.frame.size.height, KWIDTH, 260) GenderTool:CGRectMake(0, 290, KWIDTH, 30) WeightTool:CGRectMake(0, 0, KWIDTH, 30) HeightTool:CGRectMake(0, 290, KWIDTH, 30) BirthdayTool:CGRectMake(0, 290, KWIDTH, 30) CityTool:CGRectMake(0, 290, KWIDTH, 30)];
            }];
        }
        if (indexPath.row == 4)
        {
            // 弹出生日pickerView 隐藏其他pickerView
            [UIView animateWithDuration:0.5 animations:^{
                [self setAllPickerViewFrame:CGRectMake(0, KHEIGHT - 290, KWIDTH, 290) YearFrame:CGRectMake(0, 30, KWIDTH, 260) GenderFrame:CGRectMake(0, _AllPickerView.frame.size.height, KWIDTH, 260) WeightFrame: CGRectMake(0, _AllPickerView.frame.size.height, KWIDTH, 260) HeightFrame:CGRectMake(0, _AllPickerView.frame.size.height, KWIDTH, 260) CityFrame:CGRectMake(0, _AllPickerView.frame.size.height, KWIDTH, 260) GenderTool:CGRectMake(0, 350, KWIDTH, 30) WeightTool:CGRectMake(0, 350, KWIDTH, 30) HeightTool:CGRectMake(0, 350, KWIDTH, 30) BirthdayTool:CGRectMake(0, 0, KWIDTH, 30) CityTool:CGRectMake(0, 350, KWIDTH, 30)];;
            }];
        }
        if (indexPath.row == 5)
        {
            // 弹出城市pickerView 隐藏其他pickerView
            [UIView animateWithDuration:0.5 animations:^{
                [self setAllPickerViewFrame:CGRectMake(0, KHEIGHT - 290, KWIDTH, 290) YearFrame:CGRectMake(0, _AllPickerView.frame.size.height, KWIDTH, 260) GenderFrame:CGRectMake(0, _AllPickerView.frame.size.height, KWIDTH, 260) WeightFrame:CGRectMake(0, _AllPickerView.frame.size.height, KWIDTH, 260) HeightFrame:CGRectMake(0, _AllPickerView.frame.size.height, KWIDTH, 260) CityFrame:CGRectMake(0, 30, KWIDTH, 260) GenderTool:CGRectMake(0, 350, KWIDTH, 30) WeightTool:CGRectMake(0, 350, KWIDTH, 30) HeightTool:CGRectMake(0, 350, KWIDTH, 30) BirthdayTool:CGRectMake(0, 350, KWIDTH, 30) CityTool:CGRectMake(0, 0, KWIDTH, 30)];
            }];
        }
    }
}

#pragma mark --- 设置每个pickerView frame 每个toolBar frame ---
- (void)setAllPickerViewFrame:(CGRect )allPickerViewFrame
                    YearFrame:(CGRect )yearFrame
                  GenderFrame:(CGRect )genderFrame
                  WeightFrame:(CGRect )weightFrame
                  HeightFrame:(CGRect )heightFrame
                    CityFrame:(CGRect )cityFrame
                   GenderTool:(CGRect )genderTool
                   WeightTool:(CGRect )weightTool
                   HeightTool:(CGRect )heightTool
                 BirthdayTool:(CGRect )birthdayTool
                     CityTool:(CGRect )cityTool
{
    _AllPickerView.frame = allPickerViewFrame;
    _yearPickerView.frame = yearFrame;
    _genderPickerView.frame = genderFrame;
    _weightPickerView.frame = weightFrame;
    _heightPickerView.frame = heightFrame;
    _cityPickerView.frame = cityFrame;
    
    _genderToolBar.frame = genderTool;
    _weightToolBar.frame = weightTool;
    _heightToolbar.frame = heightTool;
    _birthdayToolBar.frame = birthdayTool;
    _cityToolBar.frame = cityTool;
}


#pragma mark --- 返回每个pickerView 分区个数 ---
- (NSInteger) numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    if (pickerView == _genderPickerView)
    {
        return 1;
    }
    else if (pickerView == _yearPickerView)
    {
        return 3;
    }
    else if (pickerView == _weightPickerView)
    {
        return 1;
    }
    else if (pickerView == _heightPickerView)
    {
        return 1;
    }
    else if (pickerView == _cityPickerView)
    {
        return 2;
    }
    return 0;
}

#pragma mark --- 返回每个人pickerView 分区的行数 ---
- (NSInteger) pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    if (pickerView == _genderPickerView)
    {
        if (component == 0)
        {
            return [_genderArray count];
        }
    }
    else if (pickerView == _yearPickerView)
    {
        if (component == 0)
        {
            return [_yearArray count];
        }
        if (component == 1)
        {
            return  [_monthArray count];
        }
        if (component == 2)
        {
            return [_dayArray count];
        }
    }
    else if (pickerView == _heightPickerView)
    {

        return [_heightArray count];
        
    }
    else if (pickerView == _weightPickerView)
    {
        return [_weightArray count];
    }
    else if (pickerView == _cityPickerView)
    {
        if (component == 0)
        {
            return [_provinceArray count];
        }
        if (component == 1)
        {
            NSInteger rowProvince = [_cityPickerView selectedRowInComponent:0];
            NSString *provinceName = _provinceArray[rowProvince];
            NSArray *citys = _cityDic[provinceName];
            return citys.count;
        }
    }
    return 0;
}

#pragma mark --- 返回pickerView 每个分区每行的数据 ---
- (NSString *) pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    if (pickerView == _genderPickerView)
    {
        return [_genderArray objectAtIndex:row];
    }
    else if (pickerView == _yearPickerView)
    {
        if (component == 1)
        {
            return [_monthArray objectAtIndex:row];
        }
        else if (component == 2)
        {
            return [_dayArray objectAtIndex:row];
        }
        else if (component == 0)
        {
            return [_yearArray objectAtIndex:row];
        }
    }
    else if (pickerView == _weightPickerView)
    {
        return [_weightArray objectAtIndex:row];
    }
    else if (pickerView == _heightPickerView)
    {
        return [_heightArray objectAtIndex:row];
    }
    else if (pickerView == _cityPickerView)
    {
        if (component == 0) {
            return _provinceArray[row];
        }
        if(component == 1)
        {
            NSInteger rowProvince = [_cityPickerView selectedRowInComponent:0];
            NSString *provinceName = _provinceArray[rowProvince];
            NSArray *citys = [_cityDic objectForKey:provinceName];
            return [citys objectAtIndex:row];
        }
    }
    return nil;
}

#pragma mark --- 点击pickerView 触发的方法 ---
- (void) pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    if (pickerView == _cityPickerView)
    {
        [_cityPickerView reloadComponent:1];
    }
}

#pragma mark --- 返回pickerView 的文本格式(包括高度 字体大小 颜色) ---
- (UIView *) pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
{
    if (pickerView == _genderPickerView)
   {
       UILabel *pickerLabel = [[UILabel alloc] init];
       pickerLabel.textAlignment = NSTextAlignmentCenter;
       [pickerLabel setBackgroundColor:[UIColor clearColor]];
       [pickerLabel setFont:[UIFont boldSystemFontOfSize:14]];
       pickerLabel.text=[self pickerView:pickerView titleForRow:row forComponent:component];
       return pickerLabel;
    }
    UILabel *pickerLabel = [[UILabel alloc] init];
    pickerLabel.textAlignment = NSTextAlignmentCenter;
    [pickerLabel setBackgroundColor:[UIColor clearColor]];
    pickerLabel.text=[self pickerView:pickerView titleForRow:row forComponent:component];
    return pickerLabel;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
